package com.zhiyou100.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.PublishTheProject;
import com.zhiyou100.service.ProjectsServcie;

@Controller
@RequestMapping(value="/project")
public class ProjectsController {

	@Autowired
	private ProjectsServcie projectsService;
	
	// 全部项目查询
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String Plist(@RequestParam(defaultValue="1")int pageNo,@RequestParam(defaultValue="")String dname,
			@RequestParam(defaultValue="")String ID_num,@RequestParam(defaultValue="")String section_id,Model model) {
		
		// 模糊查询关键字
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("dname",dname);
		map.put("ID_num",ID_num);
		map.put("section_id",section_id);
		
		// 使用pageHelper 进行分页 
		// 第一个参数代表当前页,第二个参数代表每页的数据记录条数
		PageHelper.startPage(pageNo, 2);
		System.out.println("查询全部,来到了Controller=========");
		List<PublishTheProject> list = projectsService.list(map);
		System.out.println("输出查询到的全部项目:"+list);
		// 查询到全部数据后,开始分页
		PageInfo<PublishTheProject> pageInfo = new PageInfo<PublishTheProject>(list);
		// 把分页后的数据(list结果集)添加进model域   并且把pageInfo对象,添加进model域
		model.addAttribute("list", pageInfo.getList());
		model.addAttribute("page",pageInfo);
		model.addAttribute("map", map);
		return "forward:/admin/products.jsp";
	}
	
	// 详情查询 
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public String Pdetail(int id,Model model) {
		System.out.println("输出接收的id:"+id);
		PublishTheProject publishTheProject =projectsService.pdetail(id);
		System.out.println("输出查询到的项目详情"+publishTheProject);
		model.addAttribute("p", publishTheProject);
		return "forward:/admin/product/product-info1.jsp";
	}
	
	// 更新项目
	
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public String Pupdate (PublishTheProject project) {
		projectsService.updateProject(project);
		return "forward:/plist";
	}
	
	// 删除项目
	
		@RequestMapping(value="/delete",method=RequestMethod.GET)
		public String Pdelete (int id) {
			projectsService.deletProject(id);
			return "forward:/plist";
		}
	
}
